package top.yueyazhui.vhr.util;

import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class FastDFSUtil {

    private StorageClient1 client1 = null;

    private void before() throws MyException, IOException {
        // 加载配置文件
        ClientGlobal.initByProperties("fastdfs-client.properties");
        // 构建TrackerClient
        TrackerClient trackerClient = new TrackerClient();
        // 获得TrackerServer
        TrackerServer trackerServer = trackerClient.getConnection();
        // 初始化StorageServer
        StorageServer storageServer = null;
        // 获得一个StorageClient1
        client1 = new StorageClient1(trackerServer, storageServer);
    }

    public String upload(byte[] bytes, String suffix) throws MyException, IOException {
        before();
        // 元数据信息（额外信息）
        NameValuePair pairs[] = null;
        // 上传
        String fileId = client1.upload_file1(bytes, suffix, pairs);
        return fileId;
    }

    public byte[] download(String url) throws MyException, IOException {
        before();
        // 下载
        return client1.download_file1(url);
    }

    public Integer delete(String url) throws MyException, IOException {
        before();
        // 删除
        Integer result = client1.delete_file1(url);
        return result;
    }
}
